var status=false;
chrome.runtime.onInstalled.addListener(function() {
  chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
    chrome.declarativeContent.onPageChanged.addRules([
      {
        conditions: [
          new chrome.declarativeContent.PageStateMatcher({
            pageUrl: {schemes:['https']},
          })
        ],
        actions: [ new chrome.declarativeContent.ShowPageAction() ]
      }
    ]);
  });
});

/*chrome.tabs.onUpdated.addListener(function(activeInfo){
     alert('ffff');
     console.log(activeInfo);
});
chrome.tabs.onActivated.addListener(function(activeInfo){
     console.log(activeInfo);
});
chrome.tabs.onSelectionChanged.addListener(function(tabId){
      //alert(tabId);
      chrome.pageAction.show(tabId);
});*/
chrome.pageAction.onClicked.addListener(function(tab){
      //alert('show click');
      chrome.pageAction.setPopup({tabId:tab.id,popup:"popup.html"});

});

var cotentport,popupport;
chrome.runtime.onConnect.addListener(function(port){
  console.log(port.name);
  if(port.name == 'popup'){
    popupport = port;
    popupport.onMessage.addListener(function(msg){
      console.log(msg);
      if(msg.command == 'on'){
        status = true;
        popupport.postMessage({command: "on"});
        cotentport.postMessage({command: "on",st:status});

      }else{
        status = false;
        popupport.postMessage({command: "off"});
        cotentport.postMessage({command: "off",st:status});
      };
    //
    });
  }else{
    cotentport = port;
    cotentport.postMessage({command: "content",st:status});
    cotentport.onMessage.addListener(function(msg){
      console.log(msg);
    });

  };
});

